package com.kinglone.back.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kinglone.back.module.model.Module;
import com.kinglone.back.module.service.ModuleService;
import com.kinglone.back.permission.service.PermissionService;
import com.kinglone.back.role.model.Role;
import com.kinglone.back.role.service.RoleService;
/**
 * 权限管理控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/back/permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ModuleService moduleService;
	
	//跳转角色绑定操作页面
	@RequestMapping("/roleBindPermission.htm")
	public String roleBindPermission(@RequestParam(value="roleId")Long roleId,Model model){
		
		model.addAttribute("roleId", roleId);
		return "back/permission/role-bind-permission";
	}
	
	/**
	 * 根据父级模块的code获取子模块信息   
	 * @return
	 */
	@RequestMapping("/getModulesByParent.htm")
	public ModelAndView getModulesByParent(Module module,@RequestParam("roleId")Long roleId,Model model){
		ModelAndView mv = new ModelAndView("back/permission/admin-operas");
		 //获取角色
		 Role role = roleService.getRoleById(roleId);
		 model.addAttribute("role", role);
		 
		 //根据父级模块的code获取子模块信息
		 List<Module> modules = moduleService.getModulesByParent(module.getId());
		 model.addAttribute("modules", modules);
		 
		 //根据角色以及二级模块的code获取权限信息  
		 List<String>  operas = permissionService.getOperasByRoleIdAndCode(roleId,module.getId());
		 model.addAttribute("operas", operas);

		return mv;
	}
	
	@RequestMapping("/bindPopedom.htm")
	public String bindPopedom(Module module,@RequestParam(value="roleId")String roleId,@RequestParam(value="operaCodes")String operaCodes){
		
		try {
			permissionService.bindPopedom(module.getId(),roleId,operaCodes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "forward:/back/permission/getModulesByParent.htm";
	}
	
}
